#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QTcpSocket>
#include <QTcpServer>
#include <QTimer>
#include <QFile>
#include <QNetworkAccessManager>
#include <downloadmanager.h>
#include <QProgressDialog>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected slots:
    void newConnection();
private slots:
    void on_openSerialButton_clicked();
    void on_openTcpButton_clicked();
    void tcp_read();
    void serial_read();
    void tcpStateChanged(QAbstractSocket::SocketState socketState);
    void timer_tmout();

    void on_typeCombox_currentIndexChanged(const QString &arg1);

    void on_action_triggered();

    void upgrade_check();
    void upgrade_progress(const QUrl &url, qint64 bytesReceived, qint64 bytesTotal);
    void upgrade_complete(const QUrl &url, bool ok, const QString file_name);
    QNetworkReply *https_req(QNetworkRequest &req, const QString &method, const QByteArray &content, uint32_t tmout);
    int download_info(const QString &product, QString *p_name, QString *p_version, QString *p_path, QString *p_md5);

private:
    void guiInitate();

    Ui::MainWindow *ui;

    QSerialPort *serialPort;    //串口
    QTcpSocket  *tcpSocket;     //tcpClient
    QTcpServer  *tcpServer;   //tcpServer
    QTimer *timer;

    QTcpSocket *srv_client; //被服务的客户端

    bool timer_start;

    QNetworkAccessManager* manager;                  //网络服务
    QTimer                 upgrade_timer;            //启动时延时一会检查软件更新
    QString                cur_version = "1.0.3";    //当前版本号
    DownloadManager        upgradesrv;               //下载服务
    QProgressDialog       *upgrade_diaog;            //下载进度
};

#endif // MAINWINDOW_H
